package com.yootk.action.data;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

@RestController
@RequestMapping("/data/*")
public class DownloadAction {

    /**
     * 文件下载
     */
    @GetMapping("download")
    public void fileDownload(HttpServletResponse response) throws Exception {
        response.setContentType("application/force-download");  // 强制下载
        response.setHeader("Content-Disposition", "attachement;filename=muyan_yootk.rar");  // 设置文件名
        Resource fileResource = new ClassPathResource("/files/muyan_yootk.rar");  // 资源加载
        InputStream input = fileResource.getInputStream();  // 获取输入流
        byte data[] = new byte[1024];
        int len = 0;
        while ((len = input.read(data)) != -1) {
            response.getOutputStream().write(data, 0, len);
        }
    }
}
